"use strict";

/*************************************************************
KODAX-CORE-PLUGIN BACKEND SCRIPTS

CUSTOM META BOX (POST)
CUSTOM META BOX (PAGE)
CUSTOM META BOX (PAGE): SOCIAL LINKS CONTROL
CUSTOM META BOX (PAGE): SOCIAL LINKS PREVIEW ICON
KNOWLEDGE BASE CONTROL


*************************************************************/

/*************************************************************
CUSTOM META BOX (POST)
*************************************************************/

	jQuery(document).ready(function($) {

		//auto generate buttons
		if ($('.auto_generate').size() > 0) {

			$('.auto_generate').on('click', function () {
				var $this = $(this);
				var autoGeneratedContent =  $this.val();
				var tagName = $this.prev().prop('tagName');
				if (tagName == "INPUT") {
					$this.prev().val(autoGeneratedContent);
				} else if (tagName == "TEXTAREA") {
					$this.prev().text('');
					$this.prev().text(autoGeneratedContent);
					console.log($this.prev().text());
				}
			});
		}

		//post format specific options

		//init
		if ($('#post-formats-select').size() > 0) {

			var td_format = $('#post-formats-select input[checked="checked"]').val();
			$('.options_post_format[data-post_format="'+ td_format + '"]').show();

			//on change
			$('#post-formats-select input').on('change', function() {
				var $this=$(this);
				td_format = $this.val();
				$('.options_post_format').slideUp();
				$('.options_post_format[data-post_format="'+ td_format + '"]').slideDown();

			});

		}

	});

/*************************************************************
CUSTOM META BOX (PAGE)
*************************************************************/

	jQuery(document).ready(function($) {
		//template specific options

		//init
		if ($('#page_template').size() > 0) {

			var selectValue = $('#page_template option[selected="selected"]').val();
			if (typeof selectValue != 'undefined') {
				selectValue = selectValue.split('.');
				$('.option_' + selectValue[0]).show();
			} else {
				$('.option_default').show();
			}

			//on change
			$('#page_template').on('change', function () {
				//first hide all
				$('.option_template_specific').hide();
				//then show relevant
				selectValue = $(this).val();
				selectValue = selectValue.split('.');
				$('.option_' + selectValue[0]).show();
			});

		}

		//options container toggle
		$('.option_heading').on('click', function(e) {
			var $this = $(this);
			$this.next('.option_content_container').slideToggle();
		})

	});

/*****************************************
CUSTOM META BOX (PAGE): SOCIAL LINKS CONTROL
*****************************************/

	jQuery(document).ready(function($) {
		if ($('.cmb_social_links').size() > 0) {

			$('.cmb_button_add_social_link').on('click', function(event) {
				var $this = $(this);
				var $socialLinksRows = $this.closest('.cmb_social_links_control').prev('.cmb_social_links').find('.cmb_social_links_row');
				var numLinks = $socialLinksRows.size();
				var memberIndex = $this.closest('.option_page-team').find('.option_content_container').index($this.closest('.option_content_container'));

				$socialLinksRows.last().clone().appendTo($this.closest('.cmb_social_links_control').prev('.cmb_social_links'));

				var $newRow = $this.closest('.cmb_social_links_control').prev('.cmb_social_links').find('.cmb_social_links_row').last();
				var newTitle = "Social link "+(numLinks+1);

				var newNameIcon = "cmb_kodax_team_member["+memberIndex+"][social_links]["+numLinks+"][0]";
				var newNameLink = "cmb_kodax_team_member["+memberIndex+"][social_links]["+numLinks+"][1]";

				//update indexes
				$newRow.find('th').html(newTitle);
				$newRow.find('.cmb_social_links_icon').attr('name', newNameIcon);
				$newRow.find('.cmb_social_links_link').attr('name', newNameLink);
				$newRow.find('.cmb_social_links_link').attr('value', '');

			});

			$('.cmb_button_remove_social_link').on('click', function(event) {
				var $this = $(this);
				var $socialLinksRows =  $this.closest('.cmb_social_links_control').prev('.cmb_social_links').find('.cmb_social_links_row');
				var numLinks = $socialLinksRows.size();

				if (numLinks > 1) {
					$socialLinksRows.last().remove();	
				}
			});

		}


	});


/*****************************************
CUSTOM META BOX (PAGE): SOCIAL LINKS PREVIEW ICON
*****************************************/

	jQuery(document).ready(function($) {
		if ($('.cmb_social_links').size() > 0) {

			$('.cmb_social_links').on('change', '.cmb_social_links_icon', function(event) {
				var $this = $(this);
				var thisValue = $this.val();
				var $socialLinksRow = $this.closest('.cmb_social_links_row');
				var $iconPreview = $socialLinksRow.find('i');

				$iconPreview.attr('class', 'fa ' + thisValue);

			});

		}


	});


/*****************************************
KNOWLEDGE BASE CONTROL
*****************************************/

	jQuery(document).ready(function($) {

		if ($('#button_add_question').size() > 0) {

			//add question
			$('#button_add_question').on('click', function(event) {
				var $this = $(this);
				var $questionContainer =  $this.closest('.option_item').prev('.question_container');
				var $questionRows = $questionContainer.find('.option_item');
				var numRows = $questionRows.size();

				$questionRows.last().clone().appendTo($questionContainer);

				var $newRow = $questionContainer.find('.option_item').last();
				var newTitle = "#"+(numRows+1);
				var newQuestionVar = "cmb_kodax_faq[questions]["+numRows+"][question]";
				var newAnswerVar = "cmb_kodax_faq[questions]["+numRows+"][answer]";

				//update indexes
				$newRow.find('label b').html(newTitle);
				$newRow.find('label').attr('for', newQuestionVar);
				$newRow.find('input').attr('id', newQuestionVar);
				$newRow.find('input').attr('name', newQuestionVar);

				$newRow.find('label').eq(1).attr('for', newAnswerVar);
				$newRow.find('textarea').attr('id', newAnswerVar);
				$newRow.find('textarea').attr('name', newAnswerVar);

			});

			//remove question
			$('#button_del_question').on('click', function(event) {
				var $this = $(this);
				var $questionRows = $this.closest('.option_item').prev('.question_container').find('.option_item');
				var numRows = $questionRows.size();

				if (numRows > 1) {
					$questionRows.last().remove();	
				}
			});

		}

	});
